package com.ranjiaqi.map;

import java.util.*;

/**
 * @ClassName TestMap
 * @Description TODO
 * @Author hp
 * @Date 2021/6/25 10:30
 * @Version 1.0
 **/
public class TestMap {
    /**
     * V put(K key, V value) 键值对存储到集合，返回的类型。
     * Map接口实现类HashMap创建对昂
     * Map<k,v>K用于键的数据类型，V用于值额数据类
     * 返回值是V，值得数据类型，都会返回null
     * 当向集合中存储了重复键，会返回被覆盖的值
     */
    public static void main(String[] args) {
//        putMap();
//        getMap();
//        containsMap();
//        removeMap();
//        clearMap();
//        valuesMap();
//        KeySet();
        entrySetMap();
    }


    public static void putMap() {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);
        System.out.println("map = " + map);

    }

    public static void getMap() {
        Map<String, Integer> map = new HashMap<String, Integer>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);
        System.out.println("map.get(\"C\") = " + map.get("C"));
    }

    public static void containsMap() {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "Hello");
        map.put(2, "World");
        map.put(3, "java");
        boolean b1 = map.containsKey(2);
        boolean b2 = map.containsValue("java");
        System.out.println("b1 = " + b1);
        System.out.println("b2 = " + b2);
    }

    public static void removeMap() {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "Hello");
        map.put(2, "World");
        map.put(3, "java");
        map.remove(2);
        System.out.println(map);
    }

    public static void clearMap() {
        Map<Integer, String> map = new HashMap<Integer, String>();
        map.put(1, "Hello");
        map.put(2, "World");
        map.put(3, "java");
        map.clear();
        System.out.println("map = " + map);
        boolean b = map.isEmpty();
        System.out.println("b = " + b);
    }

    public static void valuesMap(){
        Map<String, String> map = new HashMap<String, String>();
        map.put("1", "Hello");
        map.put("2", "World");
        map.put("3", "java");
        //map对象的方法values(),所有的值取出，存储到Collections中
        //返回Collection接口实现类
        Collection<String> stringCollection = map.values();
        for (String s : stringCollection) {
            System.out.println(s);
        }
        System.out.println(stringCollection.getClass());
    }

    public static void KeySet(){
        /**
         * KeySet()方法遍历：
         * 1.Map集合的方法keySet()获取Map中的所有键
         * 2.遍历Set集合
         * 3.取出Set集合的元素(Map集合中的键)
         * 4.Map集合的方法get()传递键获取值
         */
        Map<String,String> map = new HashMap<String, String>();
        map.put("java","20000");
        map.put("c","25000");
        map.put("c++","21000");
        map.put("php","10000");
        Set<String> stringSet = map.keySet();
        Iterator<String> iterator = stringSet.iterator();
        while (iterator.hasNext()){
            String next = iterator.next();
            System.out.println("next = " + next);
        }
    }

    public static void entrySetMap(){
        Map<String,String> map = new HashMap<String, String>();
        map.put("java","20000");
        map.put("c","25000");
        map.put("c++","21000");
        map.put("php","10000");
        Set<Map.Entry<String, String>> entries = map.entrySet();
        Iterator<Map.Entry<String, String>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<String,String> entry = iterator.next();
            System.out.println(entry.getKey()+" "+entry.getValue());

        }
    }
}
